package com.yanqu.road.dao.impl.activity.shuihuyezicard;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/22 18:08
 */
public class ShuiHuYeZiCardUserDataDaoImpl extends TempDataObjectDao<ShuiHuYeZiCardUserData> {

    public static String getShuiHuYeZiCardUserDataSql = "select * from `t_u_activity_shuihuyezicard_user_data` where `activityId`=? and `userId`=?";
    public ShuiHuYeZiCardUserData getShuiHuYeZiCardUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getShuiHuYeZiCardUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateShuiHuYeZiCardUserDataSql = "update `t_u_activity_shuihuyezicard_user_data` set integral=?,rankScore=?,pveWinningStreakNum=?,pvpWinningStreakNum=?,refreshTime=?,contGetCardTimes=?,getPropNum=?,joinRoomTimesMap=?,cardMap=?,levelWinLoseMap=?,aiWinLoseMap=?,roomWinLoseMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(ShuiHuYeZiCardUserData shuiHuYeZiCardUserData) {
        shuiHuYeZiCardUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getIntegral()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getRankScore()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getPveWinningStreakNum()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getPvpWinningStreakNum()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getContGetCardTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardUserData.getGetPropNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardUserData.getJoinRoomTimesMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardUserData.getCardMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardUserData.getLevelWinLoseMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardUserData.getAiWinLoseMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardUserData.getRoomWinLoseMap())));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardUserData.getUserId()));
        if (getDb().executeNoneQuery(updateShuiHuYeZiCardUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShuiHuYeZiCardUserDataSql = "insert into `t_u_activity_shuihuyezicard_user_data`(activityId,userId,integral,rankScore,pveWinningStreakNum,pvpWinningStreakNum,refreshTime,contGetCardTimes,getPropNum,joinRoomTimesMap,cardMap,levelWinLoseMap,aiWinLoseMap,roomWinLoseMap) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ShuiHuYeZiCardUserData shuiHuYeZiCardUserData) {
        shuiHuYeZiCardUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getIntegral()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getRankScore()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getPveWinningStreakNum()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getPvpWinningStreakNum()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuYeZiCardUserData.getContGetCardTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuYeZiCardUserData.getGetPropNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardUserData.getJoinRoomTimesMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardUserData.getCardMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardUserData.getLevelWinLoseMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardUserData.getAiWinLoseMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuYeZiCardUserData.getRoomWinLoseMap())));
        if (getDb().executeNoneQuery(insertShuiHuYeZiCardUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ShuiHuYeZiCardUserData parseData(ResultSet rs) throws SQLException {
        ShuiHuYeZiCardUserData shuiHuYeZiCardUserData = new ShuiHuYeZiCardUserData();
        shuiHuYeZiCardUserData.setActivityId(rs.getInt("activityId"));
        shuiHuYeZiCardUserData.setUserId(rs.getLong("userId"));
        shuiHuYeZiCardUserData.setIntegral(rs.getInt("integral"));
        shuiHuYeZiCardUserData.setRankScore(rs.getInt("rankScore"));
        shuiHuYeZiCardUserData.setPveWinningStreakNum(rs.getInt("pveWinningStreakNum"));
        shuiHuYeZiCardUserData.setPvpWinningStreakNum(rs.getInt("pvpWinningStreakNum"));
        shuiHuYeZiCardUserData.setRefreshTime(rs.getInt("refreshTime"));
        shuiHuYeZiCardUserData.setContGetCardTimes(rs.getInt("contGetCardTimes"));
        shuiHuYeZiCardUserData.setGetPropNum(rs.getLong("getPropNum"));
        shuiHuYeZiCardUserData.setJoinRoomTimesMap(JSON.parseObject(rs.getString("joinRoomTimesMap"), new TypeReference<Map<Integer, Integer>>() {}));
        shuiHuYeZiCardUserData.setCardMap(JSON.parseObject(rs.getString("cardMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardUserCard>>() {}));
        shuiHuYeZiCardUserData.setLevelWinLoseMap(JSON.parseObject(rs.getString("levelWinLoseMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardWinOrLoseInfo>>() {}));
        shuiHuYeZiCardUserData.setAiWinLoseMap(JSON.parseObject(rs.getString("aiWinLoseMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardWinOrLoseInfo>>() {}));
        shuiHuYeZiCardUserData.setRoomWinLoseMap(JSON.parseObject(rs.getString("roomWinLoseMap"), new TypeReference<java.util.Map<java.lang.Integer, com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardWinOrLoseInfo>>() {}));

        return shuiHuYeZiCardUserData;
    }

}
